
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>zlib --- 与 gzip 兼容的压缩 &#8212; Python 3.7.3 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.3 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="gzip --- 对 gzip 格式的支持" href="gzip.html" />
    <link rel="prev" title="数据压缩和存档" href="archiving.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/zlib.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="gzip.html" title="gzip --- 对 gzip 格式的支持"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="archiving.html" title="数据压缩和存档"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">数据压缩和存档</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-zlib">
<span id="zlib-compression-compatible-with-gzip"></span><h1><a class="reference internal" href="#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> --- 与 <strong class="program">gzip</strong> 兼容的压缩<a class="headerlink" href="#module-zlib" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>此模块为需要数据压缩的程序提供了一系列函数，用于压缩和解压缩。这些函数使用了 zlib 库。zlib 库的项目主页是 <a class="reference external" href="http://www.zlib.net">http://www.zlib.net</a>. 版本低于 1.1.3 的 zlib 与此 Python 模块之间存在已知的不兼容。1.1.3 版本的 zlib 存在一个安全漏洞，我们推荐使用 1.1.4 或更新的版本。</p>
<p>zlib 的函数有很多选项，一般需要按特定顺序使用。本文档没有覆盖全部的用法。更多详细信息请于 <a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a> 参阅官方手册。</p>
<p>要读写 <code class="docutils literal notranslate"><span class="pre">.gz</span></code> 格式的文件，请参考 <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> 模块。</p>
<p>此模块中可用的异常和函数如下：</p>
<dl class="exception">
<dt id="zlib.error">
<em class="property">exception </em><code class="descclassname">zlib.</code><code class="descname">error</code><a class="headerlink" href="#zlib.error" title="永久链接至目标">¶</a></dt>
<dd><p>在压缩或解压缩过程中发生错误时的异常。</p>
</dd></dl>

<dl class="function">
<dt id="zlib.adler32">
<code class="descclassname">zlib.</code><code class="descname">adler32</code><span class="sig-paren">(</span><em>data</em><span class="optional">[</span>, <em>value</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.adler32" title="永久链接至目标">¶</a></dt>
<dd><p>计算 <em>data</em> 的 Adler-32 校验值。(Adler-32 校验的可靠性与 CRC32 基本相当，但比计算 CRC32 更高效。) 计算的结果是一个 32 位的整数。参数 <em>value</em> 是校验时的起始值，其默认值为 1。借助参数 <em>value</em> 可为分段的输入计算校验值。此算法没有加密强度，不应用于身份验证和数字签名。此算法的目的仅为验证数据的正确性，不适合作为通用散列算法。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.0 版更改: </span>返回值永远是无符号数。要在所有的 Python 版本和平台上获得相同的值，请使用 <code class="docutils literal notranslate"><span class="pre">adler32(data)</span> <span class="pre">&amp;</span> <span class="pre">0xffffffff</span></code>。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="zlib.compress">
<code class="descclassname">zlib.</code><code class="descname">compress</code><span class="sig-paren">(</span><em>data</em>, <em>level=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.compress" title="永久链接至目标">¶</a></dt>
<dd><p>压缩 <em>data</em> 中的字节，返回含有已压缩内容的 bytes 对象。参数 <em>level</em> 为整数，可取值为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 到 <code class="docutils literal notranslate"><span class="pre">9</span></code> 或 <code class="docutils literal notranslate"><span class="pre">-1</span></code>，用于指定压缩等级。<code class="docutils literal notranslate"><span class="pre">1</span></code> (Z_BEST_SPEED) 表示最快速度和最低压缩率，<code class="docutils literal notranslate"><span class="pre">9</span></code> (Z_BEST_COMPRESSION) 表示最慢速度和最高压缩率。<code class="docutils literal notranslate"><span class="pre">0</span></code> (Z_NO_COMPRESSION) 表示不压缩。参数默认值为 <code class="docutils literal notranslate"><span class="pre">-1</span></code> (Z_DEFAULT_COMPRESSION)。Z_DEFAULT_COMPRESSION 是速度和压缩率之间的平衡 (一般相当于设压缩等级为 6)。函数发生错误时抛出 <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> 异常。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.6 版更改: </span>现在，<em>level</em> 可作为关键字参数。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="zlib.compressobj">
<code class="descclassname">zlib.</code><code class="descname">compressobj</code><span class="sig-paren">(</span><em>level=-1</em>, <em>method=DEFLATED</em>, <em>wbits=MAX_WBITS</em>, <em>memLevel=DEF_MEM_LEVEL</em>, <em>strategy=Z_DEFAULT_STRATEGY</em><span class="optional">[</span>, <em>zdict</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.compressobj" title="永久链接至目标">¶</a></dt>
<dd><p>返回一个 压缩对象，用来压缩内存中难以容下的数据流。</p>
<p>参数 <em>level</em> 为压缩等级，是整数，可取值为 <code class="docutils literal notranslate"><span class="pre">0</span></code> 到 <code class="docutils literal notranslate"><span class="pre">9</span></code> 或 <code class="docutils literal notranslate"><span class="pre">-1</span></code>。<code class="docutils literal notranslate"><span class="pre">1</span></code> (Z_BEST_SPEED) 表示最快速度和最低压缩率，<code class="docutils literal notranslate"><span class="pre">9</span></code> (Z_BEST_COMPRESSION) 表示最慢速度和最高压缩率。<code class="docutils literal notranslate"><span class="pre">0</span></code> (Z_NO_COMPRESSION) 表示不压缩。参数默认值为 <code class="docutils literal notranslate"><span class="pre">-1</span></code> (Z_DEFAULT_COMPRESSION)。Z_DEFAULT_COMPRESSION 是速度和压缩率之间的平衡 (一般相当于设压缩等级为 6)。</p>
<p><em>method</em> 表示压缩算法。现在只支持 <code class="xref py py-const docutils literal notranslate"><span class="pre">DEFLATED</span></code> 这个算法。</p>
<p>参数 <em>wbits</em> 指定压缩数据时所使用的历史缓冲区的大小 (窗口大小)，并指定压缩输出是否包含头部或尾部。参数的默认值是 <code class="docutils literal notranslate"><span class="pre">15</span></code> (MAX_WBITS)。参数的值分为几个范围：</p>
<ul class="simple">
<li>+9 到 +15：窗口大小以 2 为底的对数。即这些值对应着 512 到 32768 的窗口大小。更大的值会提供更好的压缩，同时内存开销也会更大。压缩输出会包含 zlib 特定格式的头部和尾部。</li>
<li>−9 到 −15：绝对值为窗口大小以 2 为底的对数。压缩输出仅包含压缩数据，没有头部和尾部。</li>
<li>+25 到 +31 = 16 + (9 到 15)：后 4 个比特位为窗口大小以 2 为底的对数。压缩输出包含一个基本的 <strong class="program">gzip</strong> 头部，并以校验和为尾部。</li>
</ul>
<p>参数 <em>memLevel</em> 指定内部压缩操作时所占用内存大小。参数取 <code class="docutils literal notranslate"><span class="pre">1</span></code> 到 <code class="docutils literal notranslate"><span class="pre">9</span></code>。更大的值占用更多的内存，同时速度也更快输出也更小。</p>
<p>参数 <em>strategy</em> 用于调节压缩算法。可取值为  <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_DEFAULT_STRATEGY</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FILTERED</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_HUFFMAN_ONLY</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_RLE</span></code> (zlib 1.2.0.1) 或 <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FIXED</span></code> (zlib 1.2.2.2)。</p>
<p>参数 <em>zdict</em> 指定预定义的压缩字典。它是一个字节序列 (如 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象)，其中包含用户认为要压缩的数据中可能频繁出现的子序列。频率高的子序列应当放在字典的尾部。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.3 版更改: </span>添加关键字参数 <em>zdict</em>。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="zlib.crc32">
<code class="descclassname">zlib.</code><code class="descname">crc32</code><span class="sig-paren">(</span><em>data</em><span class="optional">[</span>, <em>value</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.crc32" title="永久链接至目标">¶</a></dt>
<dd><p id="index-0">计算 <em>data</em> 的 CRC (循环冗余校验) 值。计算的结果是一个 32 位的整数。参数 <em>value</em> 是校验时的起始值，其默认值为 0。借助参数 <em>value</em> 可为分段的输入计算校验值。此算法没有加密强度，不应用于身份验证和数字签名。此算法的目的仅为验证数据的正确性，不适合作为通用散列算法。</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.0 版更改: </span>返回值永远是无符号数。要在所有的 Python 版本和平台上获得相同的值，请使用 <code class="docutils literal notranslate"><span class="pre">crc32(data)</span> <span class="pre">&amp;</span> <span class="pre">0xffffffff</span></code>。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="zlib.decompress">
<code class="descclassname">zlib.</code><code class="descname">decompress</code><span class="sig-paren">(</span><em>data</em>, <em>wbits=MAX_WBITS</em>, <em>bufsize=DEF_BUF_SIZE</em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.decompress" title="永久链接至目标">¶</a></dt>
<dd><p>解压 <em>data</em> 中的字节，返回含有已解压内容的 bytes 对象。参数 <em>wbits</em> 取决于 <em>data</em> 的格式，具体参见下边的说明。<em>bufsize</em> 为输出缓冲区的起始大小。函数发生错误时抛出 <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> 异常。</p>
<p id="decompress-wbits">The <em>wbits</em> parameter controls the size of the history buffer
(or &quot;window size&quot;), and what header and trailer format is expected.
It is similar to the parameter for <a class="reference internal" href="#zlib.compressobj" title="zlib.compressobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">compressobj()</span></code></a>, but accepts
more ranges of values:</p>
<ul class="simple">
<li>+8 to +15: The base-two logarithm of the window size.  The input
must include a zlib header and trailer.</li>
<li>0: Automatically determine the window size from the zlib header.
Only supported since zlib 1.2.3.5.</li>
<li>−8 to −15: Uses the absolute value of <em>wbits</em> as the window size
logarithm.  The input must be a raw stream with no header or trailer.</li>
<li>+24 to +31 = 16 + (8 to 15): Uses the low 4 bits of the value as
the window size logarithm.  The input must include a gzip header and
trailer.</li>
<li>+40 to +47 = 32 + (8 to 15): Uses the low 4 bits of the value as
the window size logarithm, and automatically accepts either
the zlib or gzip format.</li>
</ul>
<p>When decompressing a stream, the window size must not be smaller
than the size originally used to compress the stream; using a too-small
value may result in an <a class="reference internal" href="#zlib.error" title="zlib.error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">error</span></code></a> exception. The default <em>wbits</em> value
corresponds to the largest window size and requires a zlib header and
trailer to be included.</p>
<p><em>bufsize</em> is the initial size of the buffer used to hold decompressed data.  If
more space is required, the buffer size will be increased as needed, so you
don't have to get this value exactly right; tuning it will only save a few calls
to <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code>.</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.6 版更改: </span><em>wbits</em> and <em>bufsize</em> can be used as keyword arguments.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="zlib.decompressobj">
<code class="descclassname">zlib.</code><code class="descname">decompressobj</code><span class="sig-paren">(</span><em>wbits=MAX_WBITS</em><span class="optional">[</span>, <em>zdict</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.decompressobj" title="永久链接至目标">¶</a></dt>
<dd><p>Returns a decompression object, to be used for decompressing data streams that
won't fit into memory at once.</p>
<p>The <em>wbits</em> parameter controls the size of the history buffer (or the
&quot;window size&quot;), and what header and trailer format is expected.  It has
the same meaning as <a class="reference external" href="#decompress-wbits">described for decompress()</a>.</p>
<p>The <em>zdict</em> parameter specifies a predefined compression dictionary. If
provided, this must be the same dictionary as was used by the compressor that
produced the data that is to be decompressed.</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">If <em>zdict</em> is a mutable object (such as a <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>), you must not
modify its contents between the call to <a class="reference internal" href="#zlib.decompressobj" title="zlib.decompressobj"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompressobj()</span></code></a> and the first
call to the decompressor's <code class="docutils literal notranslate"><span class="pre">decompress()</span></code> method.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">在 3.3 版更改: </span>Added the <em>zdict</em> parameter.</p>
</div>
</dd></dl>

<p>压缩对象支持以下方法：</p>
<dl class="method">
<dt id="zlib.Compress.compress">
<code class="descclassname">Compress.</code><code class="descname">compress</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.compress" title="永久链接至目标">¶</a></dt>
<dd><p>压缩 <em>data</em> 并返回 bytes 对象，这个对象含有 <em>data</em> 的部分或全部内容的已压缩数据。所得的对象必须拼接在上一次调用 <a class="reference internal" href="#zlib.compress" title="zlib.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> 方法所得数据的后面。缓冲区中可能留存部分输入以供下一次调用。</p>
</dd></dl>

<dl class="method">
<dt id="zlib.Compress.flush">
<code class="descclassname">Compress.</code><code class="descname">flush</code><span class="sig-paren">(</span><span class="optional">[</span><em>mode</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.flush" title="永久链接至目标">¶</a></dt>
<dd><p>压缩所有缓冲区的数据并返回已压缩的数据。参数 <em>mode</em> 可以传入的常量为：<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_NO_FLUSH</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_PARTIAL_FLUSH</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_SYNC_FLUSH</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FULL_FLUSH</span></code>、<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_BLOCK</span></code> (zlib 1.2.3.4) 或 <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>。默认值为 <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>。<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code> 关闭已压缩数据流并不允许再压缩其他数据，<code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code>  以外的值皆允许这个对象继续压缩数据。调用 <a class="reference internal" href="#zlib.Compress.flush" title="zlib.Compress.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> 方法并将 <em>mode</em> 设为 <code class="xref py py-const docutils literal notranslate"><span class="pre">Z_FINISH</span></code> 后会无法再次调用 <a class="reference internal" href="#zlib.compress" title="zlib.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a>，此时只能删除这个对象。</p>
</dd></dl>

<dl class="method">
<dt id="zlib.Compress.copy">
<code class="descclassname">Compress.</code><code class="descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Compress.copy" title="永久链接至目标">¶</a></dt>
<dd><p>返回此压缩对象的一个拷贝。它可以用来高效压缩一系列拥有相同前缀的数据。</p>
</dd></dl>

<p>解压缩对象支持以下方法：</p>
<dl class="attribute">
<dt id="zlib.Decompress.unused_data">
<code class="descclassname">Decompress.</code><code class="descname">unused_data</code><a class="headerlink" href="#zlib.Decompress.unused_data" title="永久链接至目标">¶</a></dt>
<dd><p>A bytes object which contains any bytes past the end of the compressed data. That is,
this remains <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code> until the last byte that contains compression data is
available.  If the whole bytestring turned out to contain compressed data, this is
<code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code>, an empty bytes object.</p>
</dd></dl>

<dl class="attribute">
<dt id="zlib.Decompress.unconsumed_tail">
<code class="descclassname">Decompress.</code><code class="descname">unconsumed_tail</code><a class="headerlink" href="#zlib.Decompress.unconsumed_tail" title="永久链接至目标">¶</a></dt>
<dd><p>A bytes object that contains any data that was not consumed by the last
<a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> call because it exceeded the limit for the uncompressed data
buffer.  This data has not yet been seen by the zlib machinery, so you must feed
it (possibly with further data concatenated to it) back to a subsequent
<a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method call in order to get correct output.</p>
</dd></dl>

<dl class="attribute">
<dt id="zlib.Decompress.eof">
<code class="descclassname">Decompress.</code><code class="descname">eof</code><a class="headerlink" href="#zlib.Decompress.eof" title="永久链接至目标">¶</a></dt>
<dd><p>A boolean indicating whether the end of the compressed data stream has been
reached.</p>
<p>This makes it possible to distinguish between a properly-formed compressed
stream, and an incomplete or truncated one.</p>
<div class="versionadded">
<p><span class="versionmodified">3.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="zlib.Decompress.decompress">
<code class="descclassname">Decompress.</code><code class="descname">decompress</code><span class="sig-paren">(</span><em>data</em>, <em>max_length=0</em><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.decompress" title="永久链接至目标">¶</a></dt>
<dd><p>Decompress <em>data</em>, returning a bytes object containing the uncompressed data
corresponding to at least part of the data in <em>string</em>.  This data should be
concatenated to the output produced by any preceding calls to the
<a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method.  Some of the input data may be preserved in internal
buffers for later processing.</p>
<p>If the optional parameter <em>max_length</em> is non-zero then the return value will be
no longer than <em>max_length</em>. This may mean that not all of the compressed input
can be processed; and unconsumed data will be stored in the attribute
<a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unconsumed_tail</span></code></a>. This bytestring must be passed to a subsequent call to
<a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> if decompression is to continue.  If <em>max_length</em> is zero
then the whole input is decompressed, and <a class="reference internal" href="#zlib.Decompress.unconsumed_tail" title="zlib.Decompress.unconsumed_tail"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unconsumed_tail</span></code></a> is empty.</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.6 版更改: </span><em>max_length</em> can be used as a keyword argument.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="zlib.Decompress.flush">
<code class="descclassname">Decompress.</code><code class="descname">flush</code><span class="sig-paren">(</span><span class="optional">[</span><em>length</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.flush" title="永久链接至目标">¶</a></dt>
<dd><p>All pending input is processed, and a bytes object containing the remaining
uncompressed output is returned.  After calling <a class="reference internal" href="#zlib.Decompress.flush" title="zlib.Decompress.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a>, the
<a class="reference internal" href="#zlib.decompress" title="zlib.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method cannot be called again; the only realistic action is
to delete the object.</p>
<p>The optional parameter <em>length</em> sets the initial size of the output buffer.</p>
</dd></dl>

<dl class="method">
<dt id="zlib.Decompress.copy">
<code class="descclassname">Decompress.</code><code class="descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#zlib.Decompress.copy" title="永久链接至目标">¶</a></dt>
<dd><p>Returns a copy of the decompression object.  This can be used to save the state
of the decompressor midway through the data stream in order to speed up random
seeks into the stream at a future point.</p>
</dd></dl>

<p>通过下列常量可获取模块所使用的 zlib 库的版本信息：</p>
<dl class="data">
<dt id="zlib.ZLIB_VERSION">
<code class="descclassname">zlib.</code><code class="descname">ZLIB_VERSION</code><a class="headerlink" href="#zlib.ZLIB_VERSION" title="永久链接至目标">¶</a></dt>
<dd><p>构建此模块时所用的 zlib 库的版本字符串。它的值可能与运行时所加载的 zlib 不同。运行时加载的 zlib 库的版本字符串为 <a class="reference internal" href="#zlib.ZLIB_RUNTIME_VERSION" title="zlib.ZLIB_RUNTIME_VERSION"><code class="xref py py-const docutils literal notranslate"><span class="pre">ZLIB_RUNTIME_VERSION</span></code></a>。</p>
</dd></dl>

<dl class="data">
<dt id="zlib.ZLIB_RUNTIME_VERSION">
<code class="descclassname">zlib.</code><code class="descname">ZLIB_RUNTIME_VERSION</code><a class="headerlink" href="#zlib.ZLIB_RUNTIME_VERSION" title="永久链接至目标">¶</a></dt>
<dd><p>解释器所加载的 zlib 库的版本字符串。</p>
<div class="versionadded">
<p><span class="versionmodified">3.3 新版功能.</span></p>
</div>
</dd></dl>

<div class="admonition seealso">
<p class="first admonition-title">参见</p>
<dl class="last docutils">
<dt>模块 <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a></dt>
<dd>读写 <strong class="program">gzip</strong> 格式的文件。</dd>
<dt><a class="reference external" href="http://www.zlib.net">http://www.zlib.net</a></dt>
<dd>zlib 库项目主页。</dd>
<dt><a class="reference external" href="http://www.zlib.net/manual.html">http://www.zlib.net/manual.html</a></dt>
<dd>zlib 库用户手册。提供了库的许多功能的解释和用法。</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="archiving.html"
                        title="上一章">数据压缩和存档</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="gzip.html"
                        title="下一章"><code class="docutils literal notranslate"><span class="pre">gzip</span></code> --- 对 <strong class="program">gzip</strong> 格式的支持</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/zlib.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="gzip.html" title="gzip --- 对 gzip 格式的支持"
             >下一页</a> |</li>
        <li class="right" >
          <a href="archiving.html" title="数据压缩和存档"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh_CN</span>
          <span class="version_switcher_placeholder">3.7.3</span>
          <a href="../index.html">文档</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" >数据压缩和存档</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2019, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 4月 09, 2019.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>1.8.4 创建。
    </div>

  </body>
</html>